﻿using Infrastructure.Repository.Sys;
using Infrastructure;
using Model.DomainModel.Sys;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Utility;
using Model;

namespace CoreDomain.BusinessCore
{
    /// <summary>
    /// 数据列核心
    /// </summary>
    public class DataColsCore
    {
        private static SysMenuTableColsRepository _sysMenuTableColsRepository = new();

        /// <summary>
        /// 通过反射同步数据列
        /// </summary>
        public static ReceiveStatus SynchronousCols(string corporationKey,string systemKey)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            //反射
            Assembly assembly = Assembly.Load("Model");
            List<Type> types = assembly.GetTypes().Where(p => p.FullName.Contains("OutPut")).ToList<Type>();
            var currentAssembly = Assembly.LoadFrom(assembly.Location);
            var geoEntityTypes = currentAssembly.ExportedTypes.Where(p => p.FullName.StartsWith("Model.BusinessModel.OutPut")).ToList();

            //进行便利获取的所有的程序集中的类
            foreach (var geoEntityType in geoEntityTypes)
            {
                var property = geoEntityType.GetProperties();
                var i = 0;
                var dateTime = DateTime.Now;

                //取类上的自定义特性
                var menuId = string.Empty;
                object[] objs = geoEntityType.GetCustomAttributes(typeof(EnitityMappingAttribute), true);
                foreach (object obj in objs)
                {
                    EnitityMappingAttribute? attr = obj as EnitityMappingAttribute;
                    if (attr != null)
                    {
                        menuId = attr.MenuId;//菜单id
                        break;
                    }
                }

                if (!string.IsNullOrWhiteSpace(menuId))
                {
                    foreach (var item in property)
                    {
                        //描述/注释 属性上
                        DescriptionAttribute? descriptionAttribute = item.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute;
                        string propertyDescription = descriptionAttribute?.Description ?? item.Name; // 如果没有描述/注释，则使用属性名作为默认值

                        var fieldName = item.Name.Substring(0, 1).ToLower() + item.Name.Substring(1);

                        SysMenuTableCols sysMenuTableCols = new SysMenuTableCols()
                        {
                            FieldId = Guid.NewGuid().ToString(),
                            MenuId = menuId,
                            FieldEnName = fieldName,
                            CorporationKey=corporationKey,
                            SystemKey = systemKey,
                            FieldType = item.PropertyType.Name,
                            FieldCnName = propertyDescription,
                            FieldOrderBy = i++,
                            FieldWidth = 100,
                            FieldFixed = string.Empty,
                            FieldSortable=false,
                            FieldAlign="left",
                            CreateTime = dateTime,
                            CreateUser = "6A75EC49-2093-4B89-950F-65E6E72746BA",
                            IsDisplay=true,
                        };
                        var model = _sysMenuTableColsRepository.GetTableColsByMenuIdOrFieldName(menuId, item.Name);
                        
                        //没有就新增，有就修改
                        if (model == null)
                            _sysMenuTableColsRepository.Insert(sysMenuTableCols, BaseSqlRepository.sysMenuTableCols_insertSql);
                        else
                        {
                            //model.FieldCnName = propertyDescription;
                            model.FieldEnName = fieldName;
                            model.FieldType = item.PropertyType.Name;
                            _sysMenuTableColsRepository.UpdateTableCols(model);
                        }

                    }
                }
            }
            return receiveStatus;
        }
    }
}
